package com.ml.mall.service.banner.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.banner.*;
import com.ml.mall.bean.res.banner.*;
import com.ml.mall.entity.banner.SysBanner;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.banner.SysBannerMapper;
import com.ml.mall.service.banner.SysBannerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysBannerServiceImpl extends ServiceImpl<SysBannerMapper, SysBanner> implements SysBannerService {
    @Autowired
    private SysBannerMapper sysBannerMapper;

    @Override
    public int addBanner(SysBannerAddDto dto) {
        //判断是否该类型的banner数是否达到上限
        QueryWrapper<SysBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 0)
                .eq("url_type", dto.getUrlType());
        if(count(queryWrapper) >= 5){
            throw new BizException(CommonEnum.INSERT_MAX_ERROR);
        }

        SysBanner sysBanner = new SysBanner();
        BeanUtils.copyProperties(dto, sysBanner);
        //执行添加操作
        int insert = sysBannerMapper.insert(sysBanner);
        if (insert==0){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return insert;
    }

    @Override
    public int delBanner(SysBannerDelDto dto) {
        List<Long> delBannerList = Arrays.asList(dto.getId());
        int deleteBatchIds = sysBannerMapper.deleteBatchIds(delBannerList);
        if (deleteBatchIds==0){
            throw new BizException(CommonEnum.DELETE_ERROR);
        }
        return deleteBatchIds;
    }

    @Override
    public BannerQueryRes queryBanner() {
        //用于最终结果输出
        BannerQueryRes bannerQueryRes = new BannerQueryRes();
        //用于封装recommendArr相关信息
        List<RecommendArrRes> recommendArrResList = new ArrayList<>();
        //用于封装titleArr相关信息
        List<TitleArrRes> titleArrResList = new ArrayList<>();
        //查询titleArr的相关信息
        QueryWrapper<SysBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url_type",0)
                .orderByAsc("index_no")
                .eq("state",0)
                .last("limit 5");
        List<SysBanner> sysBanners = sysBannerMapper.selectList(queryWrapper);
        //查询recommend的相关信息
        QueryWrapper<SysBanner> recommendQueryWrapper = new QueryWrapper<>();
        recommendQueryWrapper.eq("url_type",0)
                .orderByAsc("index_no")
                .orderByDesc("click_num")
                .eq("state",0)
                .last("limit 5");
        List<SysBanner> sysBannerList = sysBannerMapper.selectList(recommendQueryWrapper);
        //进行title数据的封装
        for (SysBanner sysBanner:sysBanners){
            TitleArrRes titleArr = new TitleArrRes();

            titleArr.setPicUrl(sysBanner.getPicUrl())
                    .setUrlType(sysBanner.getUrlType())
                    .setTitle(sysBanner.getTitle())
                    .setIndexNo(sysBanner.getIndexNo());
            //list集合封装
            titleArrResList.add(titleArr);
        }
        //进行recommend数据封装
        for (SysBanner sysBanner:sysBannerList){
            RecommendArrRes recommendArr = new RecommendArrRes();
            recommendArr.setId(sysBanner.getId())
                    .setPicUrl(sysBanner.getPicUrl())
                    .setLinkUrl(sysBanner.getLinkUrl())
                    .setIndexNo(sysBanner.getIndexNo())
                    .setClickNum(sysBanner.getClickNum())
                    .setTitle(sysBanner.getTitle())
                    .setUrlType(sysBanner.getUrlType());
            //list集合封装
            recommendArrResList.add(recommendArr);
        }
        //最终结果封装
        bannerQueryRes.setTitleArr(titleArrResList);
        bannerQueryRes.setRecommendArrRes(recommendArrResList);
        return bannerQueryRes;
    }

    @Override
    public int updBanner(SysBannerEditDto dto) {
        //禁用状态下也支持修改
        SysBanner sysBanner = new SysBanner();
        sysBanner.setId(dto.getId())
                .setPicUrl(dto.getPicUrl())
                .setLinkUrl(dto.getLinkUrl())
                .setIndexNo(dto.getIndexNo())
                .setTitle(dto.getTitle());
        int updateById = sysBannerMapper.updateById(sysBanner);
        if (updateById==0){
            throw new BizException(CommonEnum.UPDATE_ERROR);
        }
        return updateById;
    }

    @Override
    public int clickBanner(SysBannerClickDto dto) {
        //首先要知道原有的点击量
        int clickNum = sysBannerMapper.selectClickNum(dto.getId());
        clickNum += 1;
        //执行更新点击量操作
        int updateClickNum = sysBannerMapper.updateClickNum(clickNum, dto.getId());
        if (updateClickNum==0){
            throw new BizException(CommonEnum.UPDATE_ERROR);
        }
        return updateClickNum;
    }

    @Override
    public BannerQueryByTypeRes queryByType(SysBannerQueryByTypeDto dto) {
        //最终输出
        BannerQueryByTypeRes queryByTypeRes = new BannerQueryByTypeRes();

        //查询banner
        QueryWrapper<SysBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", 0)
                .eq("url_type", dto.getUrlType())
                .orderByAsc("index_no")
                .orderByDesc("click_num")
                .last("limit 5");
        List<SysBanner> sysBanners = sysBannerMapper.selectList(queryWrapper);
        //数据封装
        if(null != sysBanners){
            List<BannerRes> bannerArr = sysBanners.stream()
                    .map(sysBanner -> {
                        BannerRes bannerRes = new BannerRes();
                        BeanUtils.copyProperties(sysBanner, bannerRes);
                        return bannerRes;
                    })
                    .collect(Collectors.toList());
            queryByTypeRes.setBannerArr(bannerArr);
        }
        return queryByTypeRes;
    }
}
